class Admin::BonusRecordsController < AdminController
  before_action :set_user

  def index
    @bonus_records = @user.bonus_records.order("give_at DESC").page(params[:page]).per(params[:per])
  end

  def new
    @bonus_record = BonusRecord.new
  end

  def create
    @bonus_record = @user.bonus_records.build bonus_record_params
    @bonus_record.admin_id = current_admin.id
    if @bonus_record.save
      redirect_to admin_user_bonus_records_path(@user), notice: "保存成功！"
    else
      redirect_to admin_user_bonus_records_path(@user), notice: "保存失败！"
    end
  end

  def edit
    @bonus_record = BonusRecord.find params[:id]
  end

  def update
    @bonus_record = BonusRecord.find params[:id]
    if @bonus_record.update bonus_record_params
      redirect_to admin_user_bonus_records_path(@user), notice: "更新成功！"
    else
      redirect_to admin_user_bonus_records_path(@user), notice: "更新失败！"
    end
  end
  
  def destroy
    @bonus_record = BonusRecord.find params[:id]
    if @bonus_record.destroy
      redirect_to admin_user_bonus_records_path(@user), notice: "删除成功."
    else
      redirect_to admin_user_bonus_records_path(@user), notice: "删除失败."
    end
  end

  private

  def set_user
    @user = User.find params[:user_id]
  end

  def bonus_record_params
    params.require(:bonus_record).permit(:admin_id, :user_id, :order_count, :bonus, :give_at, :content)
  end
end
